package com.xtaller.easy.admin.mapper;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.xtaller.easy.admin.model.BaseUser;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
* Created by Taller on 2018/04/13
*/
@Mapper 
public interface BaseUserMapper extends BaseMapper<BaseUser> {
    @Select("SELECT a.id,a.loginName,a.status FROM base_user a " +
            "JOIN (SELECT DISTINCT userId from v_user_label where 1=1 ${where} " +
            "LIMIT #{index}, #{size})b ON a.id=b.userId order by `createTime` desc ")
    List<JSONObject> getPage(@Param("where") String where,
                             @Param("index") int index,
                             @Param("size") int size);

    @Select("select count(DISTINCT userId) total from v_user_label where 1=1 ${where} ")
    JSONObject getPageCount(@Param("where") String where);

    @Select("Select * from v_user_label where userId in (${ids})")
    List<JSONObject> getLabelByUserIds(@Param("ids") String ids);

    @Select("Select * from v_user_role where userId in (${ids})")
    List<JSONObject> getRoleByUserIds(@Param("ids") String ids);

    @Update("delete from base_user_role where userId=#{userId} ")
    Boolean initUserRole(@Param("userId") String userId);

    @Select("Select * from v_user_role_module where code=#{w.code} and userId=#{w.userId}")
    List<JSONObject> getRoleByCode(@Param("w") JSONObject where);

    @Select("select a.roleId,b.id,a.menuId,b.name,b.api,b.icon,b.sort,b.url,b.isHidden, " +
            "   a.authId,c.name btnName,a.isMenu,c.icon btnIcon,c.fun,c.isBtn,c.method,c.sort btnSort,d.api btnApi, " +
            "   b.parentId " +
            "from sys_role_auth a " +
            "   left join sys_menu b on b.id=a.menuId and b.moduleId=(select id from sys_module where code=#{w.code}) " +
            "   left join sys_button c on c.id=authId " +
            "   left join sys_menu_auth d on d.buttonId=a.authId and d.menuId=b.id and d.isDel=0 " +
            "where a.roleId in (${w.roleIds}) and a.isDel=0 and b.isDel=0  " +
            "order by b.sort, c.sort;")
    List<JSONObject> getAuthByRoleIds(@Param("w") JSONObject where);

}